import Tpl from './index.ejs';
import PinTpl from './pin.ejs';
import PinUpdateTpl from './pinUpdate.ejs';
import ZhuanTpl from './zhuan.ejs';
import ZhuanUpdateTpl from './zhuanUpdate.ejs';
import ListTpl from './list.ejs';

import Tools from '../tools/tools.js';

class Platform {

	init ($wrap, router) {
		let self = this;

		this.render($wrap, router, function () {

			$('.ui.checkbox').checkbox();
	        $('.dropdown').dropdown();

			self.$listWrap = $('#listWrap');
			self.reqData = {};

			self.bindEvent();
		});
	}

	bindEvent () {
		let self = this;

		$('#pinCheckBtn').on('click', function () {
			self.pinCheckForm();
		});

		$('#zhuanCheckBtn').on('click', function () {
			self.zhuanCheckForm();
		});
	}

	update ($ele) {
		let self = this;
		let reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
        }, Tools.getFromData());
		reqData[$ele.data('key')] = $ele.data('id');

        $.ajax({
			url: '/product/info/showDetail',
			method: 'post',
			data: JSON.stringify(reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					if (self.reqData.type === 1) {
						$('#pinUpdate').html(PinUpdateTpl(data));
						$('#pinUpdateBtn').on('click', function () {
							self.pinUpdateFrom();
						});
					}
					else {
						$('#zhuanUpdate').html(ZhuanUpdateTpl(data));
						$('#zhuanUpdateBtn').on('click', function () {
							self.zhuanUpdateFrom();
						});
					}
				}
			}
        });
	}

	pinCheckForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 1
        }, Tools.getFromData());

        $.ajax({
			url: '/product/info/queryProductList',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					Tools.getPagination({
						data: self.reqData,
						url: '/product/info/queryProductList',
						wrap: self.$listWrap,
						callback: function (data) {

							self.$listWrap.html(ListTpl(data));
							$('.update').on('click', function () {
								self.update($(this));
							});
						}
					});
				}
			}
        });
	}

	pinUpdateFrom () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 1,
			carProductModels: Tools.getCarFromData()
        }, Tools.getFromData());

        $.ajax({
			url: '/product/info/updateProduct',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					alert('修改成功');
				}
				else {
					alert('修改失败');
				}
			}
        });
	}

	zhuanCheckForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 2
        }, Tools.getFromData());

        $.ajax({
			url: '/product/info/queryProductList',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					self.$listWrap.html(ListTpl(data));

					$('.update').on('click', function () {
						self.update($(this));
					});
				}
			}
        });
	}

	zhuanUpdateFrom () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: 2
        }, Tools.getFromData());

        $.ajax({
			url: '/product/info/updateProduct',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					alert('修改成功');
				}
				else {
					alert('修改失败');
				}
			}
        });
	}

	render ($wrap, router, callback) {
		if (!router) {
            $wrap.html(Tpl());
        }
        else {
            switch (router) {
               	case 'pin':

					Tools.getCity(function (data) {
						$wrap.html(PinTpl(data));
						callback();
					});
               		break;
                case 'zhuan':
               		$wrap.html(ZhuanTpl());
					callback();
               		break;
               	default:
               		break;
            }
        }
	}
}

export default new Platform();
